'''
Created on 29.07.2010

@author: Valiuh Anton - valiuh@gmail.com
'''

import Image

from VideoCapture import Device

class PreImage (object):
    '''
        This class using for prepare input image.
        It convert RGB input image into YCbCr color 
        space. And then transform a color image from 
        that color space into a grey scale image such 
        that the gray value at each pixel shows the 
        likelihood of the pixel belonging to the skin.
        input_image - buffer for input image
        width, height - sizes of input image
        output_YCbCr_color_space - buffer for image which 
                             contain YCbCr color space
        output_YCbCr_grey_scale - buffer for grey scale image                                        
    '''
    def __init__ (self,image):
        
        self.input_image = image
        self.width, self.height = self.input_image.size
        size = self.input_image.size
        self.output_YCbCr_color_space = Image.new("RGB", size)
        self.output_YCbCr_grey_scale = Image.new("L", size)
        print "\t==== class PreImage init ok ===="
    #=============================================================    
    def RGB2YCbCr (self,rgb):
        '''
            Convert one pixel from RGB to YCbCr                                   
        '''        
        cbcr = [0,0,0]
        cbcr[0] =  int(0.299*rgb[0] + 0.587*rgb[1] + 0.114*rgb[2]) #Y
        cbcr[1] =  int((rgb[2] - cbcr[0])*0.564 + 128) #Cb
        cbcr[2] =  int((rgb[0] - cbcr[0])*0.713 + 128) #Cr
        return cbcr
    #=============================================================    
    def YCbCr2GREY (self,rgb):
        '''
            Convert one pixel from YCbCr to grey skin color model                                   
        '''        
      
        if ((rgb[1]>90)&(rgb[1]<160))&((rgb[2]>90)&(rgb[2]<160)):
            grey = 0
        else:
            grey = 255
        return grey      
    #=============================================================
    def convert_to_YCbCr (self):
        '''
           Convert all image from RGB to YCbCr and than to 
           grey scale image.                                 
        '''    
        print "\t==== Begining convert to YCbCr... ===="
        cbcr = [0,0,0]
        for i in range(self.height):
            for j in range (self.width):
                rgb = self.input_image.getpixel((j,i))
                cbcr = self.RGB2YCbCr(rgb)
                self.output_YCbCr_color_space.putpixel((j,i),tuple(cbcr))
                                                    
                self.YCbCr_to_grey_scale(j,i,cbcr)
                                                   
        print "\t==== converting to YCbCr ok ===="
        print "\t==== converting from YCbCr to grey scale ok ===="
    
    #=============================================================
    def YCbCr_to_grey_scale(self,x,y,rgb):
        '''
           Put one pixel into grey scale image.                                 
        '''      
        grey = self.YCbCr2GREY(rgb)
        self.output_YCbCr_grey_scale.putpixel((x,y),grey)
        
    #===============================================================
    def convert (self, mode, color_space):
        '''
            Main function for convert from RGB image 
            to YCbCr color space                                  
        '''    
        if color_space == 'YCbCr':
            self.convert_to_YCbCr()
            if mode == 's':
                self.output_YCbCr_color_space.show()
            elif mode == 'w':
                self.output_YCbCr_color_space.save("temp_cs_YCbCr.jpg", "JPEG")
            elif mode == 'sw':
                self.output_YCbCr_color_space.show()
                self.output_YCbCr_color_space.save("temp_cs_YCbCr.jpg", "JPEG")
            elif mode == '':
                print ""
            else:
                print "Invalid mode. mode mast be s, w, or sw"
        else: 
            print "Invalid color space. It most be YCbCr"
    #==========================================================
    def grey_scale (self, mode, color_space):
        '''
            Main function for convert from YCbCr color space 
            image to grey skin color model image                                 
        '''    
        if color_space == 'YCbCr':
            
            if mode == 's':
                self.output_YCbCr_grey_scale.show()
            elif mode == 'w':
                self.output_YCbCr_grey_scale.save("temp_gr_YCbCr.jpg", "JPEG")
            elif mode == 'sw':
                self.output_YCbCr_grey_scale.show()
                self.output_YCbCr_grey_scale.save("temp_gr_YCbCr.jpg", "JPEG")
            elif mode == '':
                print ""
            else:
                print "Invalid mode. mode mast be s, w, or sw"
        else: 
            print "Invalid color space. It most be YCbCr"
        
        


